package org.example.aidemo.service.impl;

import org.example.aidemo.mapper.OrderMapper;
import org.example.aidemo.pojo.Order;
import org.example.aidemo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Order getOrderById(String id) {
        return orderMapper.selectById(id);
    }

    @Override
    public List<Order> getAllOrders() {
        return orderMapper.selectList(null);
    }
    @Override
    public List<Order> getOrderByUsername(String username){
        return orderMapper.selectByUsername(username);
    }
    @Override
    public Order deleteOrderById(String id){
        // 先查询订单
        Order order = getOrderById(id);
        if (order != null) {
            // 再执行删除操作
            orderMapper.deleteById(id);
            return order; // 返回查询到的对象
        }
        return null;
    }
    @Override
    public Order modifyOrderByIdToAddress(String id, String address){
        // 先查询订单
        Order order = getOrderById(id);
        if (order != null) {
            int a=orderMapper.modifyOrderByIdToAddress(id,address);
            if(a!=0){
                return order;
            }
        }
        return null;
    }
    @Override
    public int addOrder(String name, String username, String address, LocalDateTime createTime){
        return orderMapper.addOrder(
                UUID.randomUUID().toString(),name,username,address,createTime);
    }

}
